﻿using MediatR;
using W.Purchase.API.Application.Command.SaleReturnCommand;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.SaleReturnCommandHandler
{
    public class SaleDeleteReturnCommandHandler:IRequestHandler<SaleDeleteReturnCommand,int>
    {
        private readonly ISaleReturnsReposiotry saleReturnsReposiotry;

        public SaleDeleteReturnCommandHandler(ISaleReturnsReposiotry saleReturnsReposiotry)
        {
            this.saleReturnsReposiotry = saleReturnsReposiotry;
        }

        async Task<int> IRequestHandler<SaleDeleteReturnCommand, int>.Handle(SaleDeleteReturnCommand request, CancellationToken cancellationToken)
        {
            var list = (await saleReturnsReposiotry.GetAllAsaync()).Where(w=>request.SaleReturnsId.Contains(w.SaleReturnsId)).ToList();
            list.ForEach(w =>
            {
                w.IsDelete = true;
                w.DeleteTime = DateTime.Now;
            });
            return await saleReturnsReposiotry.UpdateRangeAsync(list);
        }
    }
}
